﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Edit_RobotOrderDetail : Form
    {        
        private INISettings mySetting;
        private SqlConnection myConnection;

        public string serial;

        public frm_Edit_RobotOrderDetail()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.serial = null;
            getCompList();
        }
        
        ////////////////////////////////////////////////
        /// Load Comp List to form
        ////////////////////////////////////////////////
        private void getCompList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CompID FROM [231_Comp] ORDER BY CompID";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["CompID"].ToString().Trim();
                    item.Value = dataReader["CompID"].ToString().Trim();
                    this.compID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch //(Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                //MessageBox.Show(ex.Message);
                MessageBox.Show("Cannot get Comp list");
            }
        }

        /// <summary>
        /// Load Data to Edit
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public bool LoadData()
        {
            bool ret = true;
            if (string.IsNullOrEmpty(this.serial))
            {
                ret = false;
            }
            else
            {
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "SELECT SerialNo, PlanNo, OrderNo, CompID, Quantity, Remark"
                        + " FROM [231_RobotOrderDetail] WHERE SerialNo = '" + this.serial + "'";
                    SqlDataReader dataReader = sql_cmd.ExecuteReader();
                    if (dataReader.Read())
                    {
                        for (int i = 0; i < this.compID.Properties.Items.Count; i++)
                        {
                            ComboboxItem item = (ComboboxItem)this.compID.Properties.Items[i];
                            if (item.Value.ToString().Trim() == dataReader["CompID"].ToString().Trim())
                            {
                                this.compID.SelectedIndex = i;
                            }
                        }

                        this.serialNo.Text = dataReader["SerialNo"].ToString().Trim();
                        this.planNo.Text = dataReader["PlanNo"].ToString().Trim();
                        this.orderNo.Text = dataReader["OrderNo"].ToString().Trim();
                        this.quantityValue.Text = dataReader["Quantity"].ToString();
                        this.remarkText.Text = dataReader["Remark"].ToString().Trim();
                    }
                    sql_cmd.Dispose();
                    myConnection.Close();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    ret = false;
                }
            }
            return ret;
        }


        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string err = null;
            string new_serial = null;
            string new_plan = null;
            string new_order = null;
            string new_comp = null;
            float new_quantity = 0;
            string new_remark = null;

            if (this.compID.SelectedIndex < 0)
            {
                err = "Please select Comp";
            }
            else
            {
                ComboboxItem comp = (ComboboxItem)this.compID.SelectedItem;
                new_comp = comp.Value.ToString();
            }

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.serialNo.Text))
                {
                    err = "Plase enter Serial No";
                }
                else
                {
                    new_serial = this.serialNo.Text;
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.planNo.Text))
                {
                    err = "Plase enter Plan No";
                }
                else
                {
                    new_plan = this.planNo.Text;
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (string.IsNullOrEmpty(this.orderNo.Text))
                {
                    err = "Plase enter Order No";
                }
                else
                {
                    new_order = this.orderNo.Text;
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                if (float.TryParse(this.quantityValue.Text.Trim(), out new_quantity))
                {
                    if (new_quantity <= 0)
                    {
                        err = "Invalid Quantity";
                    }
                }
                else
                {
                    err = "Invalid Quantity";
                }
            }

            if (string.IsNullOrEmpty(err))
            {
                new_remark = this.remarkText.Text.Trim();
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "UPDATE [231_RobotOrderDetail] SET SerialNo = '" + new_serial + "',"
                        + " PlanNo = '" + new_plan + "', OrderNo = '" + new_order + "', CompID = '" + new_comp + "',"
                        + " Quantity = " + new_quantity + ", Remark = N'" + new_remark + "'"
                        + " WHERE SerialNo = '" + this.serial + "'";
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Robot Order Details update successfully");
                    this.Dispose();
                }
                catch
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show("Cannot update Robot Order Detail");
                }
            }
            else
            {
                MessageBox.Show(err);
            }
        }             
    }
}
